#-*- coding: utf-8 -*-
#
# FileName: ModifyIP.py
# Date    : 2016-02-18
#

import wmi

print 'It is running!'

wmiService = wmi.WMI()
colNicConfigs = wmiService.Win32_NetworkAdapterConfiguration(IPEnabled = True)

#for objNicConfig in colNicConfigs:
#    print objNicConfig.Index
#    print objNicConfig.SettingID
#    print objNicConfig.Description.encode("cp936")
#    print objNicConfig.IPAddress
#    print objNicConfig.IPSubnet
#    print objNicConfig.DefaultIPGateway
#    print objNicConfig.DNSServerSearchOrder

if len(colNicConfigs) < 1:
    print 'No Useable Networking'
    exit()
objNicConfig = colNicConfigs[0]

#for method_name in objNicConfig.methods:
#    method = getattr(objNicConfig, method_name)
#    print method

arrIPAddresses = ['192.168.1.10']
arrSubnetMasks = ['255.255.255.0']
arrDefaultGateways = ['192.168.1.2']
arrGatewayCostMetrics = [1]
arrDNSServers = ['192.168.1.2']
intReboot = 0

returnValue = objNicConfig.EnableStatic(IPAddress = arrIPAddresses, SubnetMask = arrSubnetMasks)

if returnValue[0] == 0:
    print 'Set IP Success'
elif returnValue[0] == 1:
    print 'Set IP Success'
    intReboot += 1
else:
    print 'Set IP Error'
    exit()
returnValue = objNicConfig.SetGateways(DefaultIPGateway = arrDefaultGateways, GatewayCostMetric = arrGatewayCostMetrics)
if returnValue[0] == 0:
    print 'Set Gateway Success'
elif returnValue[0] == 1:
    print 'Set Gateway Success'
    intReboot += 1
else:
    print 'Set IP Error:Gateway Error'
    exit()

returnValue = objNicConfig.SetDNSServerSearchOrder(DNSServerSearchOrder = arrDNSServers)
if returnValue[0] == 0:
    print 'Set DNS Success'
elif returnValue[0] == 1:
    print 'Set DNS Success'
    intReboot += 1
else:
    print 'Set IP: DNS Error'
    exit()

if intReboot > 0:
    print 'Need Computer Reboot'
else:
    print ''
    print 'Configure：'
    print 'IP: ', ', '.join(objNicConfig.IPAddress)
    print 'Netmask: ', ', '.join(objNicConfig.IPSubnet)
    print 'Gateway: ', ', '.join(objNicConfig.DefaultIPGateway)
    print 'DNS: ', ', '.join(objNicConfig.DNSServerSearchOrder)

print 'Set IP Success'